package Cantella::DJabberd::Store::Result::ExternalComponent;

use base qw'DBIx::Class::Core';
use metaclass 'Reaction::Meta::Class';
use Moose;

use MooseX::Types::Moose qw/ArrayRef Int/;
use Reaction::Types::Core qw/NonEmptySimpleStr/;

use aliased 'Cantella::DJabberd::Store::Result::Vhost';

use namespace::clean -except => [ 'meta' ];

#revalidate hostname here
has id       => ( is => 'ro', required => 1, isa => Int );
has hostname => ( is => 'rw', required => 1, isa => NonEmptySimpleStr );
has port     => ( is => 'rw', required => 1, isa => Int );
has secret   => ( is => 'rw', required => 1, isa => NonEmptySimpleStr );
has vhost    => ( is => 'rw', required => 1, isa => Vhost );

__PACKAGE__->table('external_components');
__PACKAGE__->add_columns
  (
   id       => {
                data_type => "INT",
                size => 10,
                default_value => undef,
                is_nullable => 0,
                is_auto_increment => 1,
                extra => { unsigned => 1 },
               },
   vhost_id => {
                data_type => "INT",
                size => 10,
                is_nullable    => 0,
                is_foreign_key => 1,
                extra => { unsigned => 1 },
               },
   hostname => {
                data_type => "VARCHAR",
                size => 255,
                default_value => "",
                is_nullable => 0,
               },
   port     => {
                data_type => "INT",
                size => 11,
                is_nullable => 0,
               },
   secret   => {
                data_type => "VARCHAR",
                size => 255,
                default_value => "",
                is_nullable => 0,
               },
  );

__PACKAGE__->set_primary_key('id');
__PACKAGE__->add_unique_constraint(vhost_hostname => [qw/vhost_id hostname/]);
__PACKAGE__->add_unique_constraint(port => ['port']);

__PACKAGE__->belongs_to(vhost => Vhost, { 'foreign.id' => 'self.vhost_id' });

sub display_name {
  my $self = shift;
  join '', $self->hostname, '.', $self->vhost->hostname, ':', $self->port;
}

__PACKAGE__->meta->make_immutable(inline_constructor => 0);

1;
